package com.cn.ZhiKeDianShang.util.TimeSelect;

import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.app.Dialog;
import android.content.Context;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.cn.ZhiKeDianShang.R;
import com.cn.ZhiKeDianShang.model.response.StateAndTeamBean;

import java.util.ArrayList;
import java.util.List;

/**
 * 创建人：lwd
 * 时  间：2018/4/24 10:12
 */
public class CustomTeamAndStatePicker {

    //选中的aid
    String aid;
    //选中的相对应的名字
    String nickname;


    /**
     * 定义结果回调接口
     */
    public interface ResultHandler {
        void handle(String aid, String nickname, boolean isSelectTeam);
    }

    public enum SCROLL_TYPE {
        HOUR(1),
        MINUTE(2);

        SCROLL_TYPE(int value) {
            this.value = value;
        }

        public int value;
    }

    private int scrollUnits = SCROLL_TYPE.HOUR.value + SCROLL_TYPE.MINUTE.value;
    private ResultHandler handler;
    private Context context;
    private boolean canAccess = false;

    private Dialog datePickerDialog;
    private TameAndStatePickerView year_pv;

    private List<StateAndTeamBean> year;
    private TextView tv_cancle, tv_select;
    List<StateAndTeamBean> mdata;
    String selecttext;

    boolean isSelectTeam;


    public CustomTeamAndStatePicker(Context context, boolean isSelectTeam, String selecttext, List<StateAndTeamBean> data, ResultHandler resultHandler) {
        canAccess = true;
        this.selecttext = selecttext;
        this.isSelectTeam = isSelectTeam;
        this.context = context;
        this.handler = resultHandler;
        this.mdata = data;
        initDialog();
        initView();
    }

    private void initDialog() {
        if (datePickerDialog == null) {
            datePickerDialog = new Dialog(context, R.style.time_dialog);
            datePickerDialog.setCancelable(false);
            datePickerDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
            datePickerDialog.setContentView(R.layout.custom_teamandstate_picker);
            Window window = datePickerDialog.getWindow();
            window.setGravity(Gravity.BOTTOM);
            datePickerDialog.setCanceledOnTouchOutside(true);
            WindowManager manager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
            DisplayMetrics dm = new DisplayMetrics();
            manager.getDefaultDisplay().getMetrics(dm);
//            window.setWindowAnimations(R.style.dialogstyle); // 添加动画
            WindowManager.LayoutParams lp = window.getAttributes();
            lp.width = dm.widthPixels;
            window.setAttributes(lp);
        }
    }

    private void initView() {
        year_pv = (TameAndStatePickerView) datePickerDialog.findViewById(R.id.year_pv);
        tv_cancle = (TextView) datePickerDialog.findViewById(R.id.tv_cancle);
        tv_select = (TextView) datePickerDialog.findViewById(R.id.tv_select);

        tv_cancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                datePickerDialog.dismiss();
            }
        });

        tv_select.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                handler.handle(aid, nickname, isSelectTeam);
                datePickerDialog.dismiss();
            }
        });
    }


    private void initTimer() {
        if (year == null) year = new ArrayList<>();
        year.clear();
        year = mdata;
        loadComponent();
    }

    // 下面操作是 列表中那个作为选中状态显示
    private void loadComponent() {
        year_pv.setData(year);
        for (int i = 0; i < mdata.size(); i++) {
            if (mdata.get(i).getNickname().equals(selecttext)) {
//                //选中的aid
//                String aid;
//                //选中的相对应的名字
//                String nickname;
                aid = mdata.get(i).getAid() + "";
                nickname = mdata.get(i).getNickname();
                //设置选中位置
                year_pv.setSelected(i);
                break;
            } else {
                //不设置位置默认显示中间的
                int monthSelected = mdata.size() / 4;
                aid = mdata.get(monthSelected).getAid() + "";
                nickname = mdata.get(monthSelected).getNickname() + "";
            }
        }
        executeScroll();
    }

    private void addListener() {
        year_pv.setOnSelectListener(new TameAndStatePickerView.onSelectListener() {
            @Override
            public void onSelect(StateAndTeamBean selectdata) {
                aid = selectdata.getAid() + "";
                nickname = selectdata.getNickname() + "";
            }
        });
    }


    private void executeAnimator(View view) {
        PropertyValuesHolder pvhX = PropertyValuesHolder.ofFloat("alpha", 1f, 0f, 1f);
        PropertyValuesHolder pvhY = PropertyValuesHolder.ofFloat("scaleX", 1f, 1.3f, 1f);
        PropertyValuesHolder pvhZ = PropertyValuesHolder.ofFloat("scaleY", 1f, 1.3f, 1f);
        ObjectAnimator.ofPropertyValuesHolder(view, pvhX, pvhY, pvhZ).setDuration(200).start();
    }

    private void executeScroll() {
        year_pv.setCanScroll(year.size() > 1);
    }

    private int disScrollUnit(SCROLL_TYPE... scroll_types) {
        if (scroll_types == null || scroll_types.length == 0) {
            scrollUnits = SCROLL_TYPE.HOUR.value + SCROLL_TYPE.MINUTE.value;
        } else {
            for (SCROLL_TYPE scroll_type : scroll_types) {
                scrollUnits ^= scroll_type.value;
            }
        }
        return scrollUnits;
    }

    public void show(String time) {
        if (canAccess) {
            canAccess = true;
            initTimer();
            //滑动监听
            addListener();
            datePickerDialog.show();
        }
    }


    /**
     * 设置日期控件是否可以循环滚动
     */
    public void setIsLoop() {
        if (canAccess) {
            this.year_pv.setIsLoop(false);
        }
    }


}